package com.last.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.last.entity.DataDTO;
import com.last.entity.PageResult;
import com.last.entity.Result;
import com.last.entity.RoomInfo;
import com.last.mapper.RoomInfoMapper;
import com.last.service.IRoomInfoService;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo> implements IRoomInfoService {

    @Value("${topic.es}")
    private String esTopicName;

    @Value("${topic.page}")
    private String pageTopicName;

    @Autowired(required = false)
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public PageResult<RoomInfo> search(DataDTO<RoomInfo> dataDTO) {
        IPage<RoomInfo> iPage = new Page<>(dataDTO.getPage(), dataDTO.getLimit());
        IPage<RoomInfo> pageInfo = roomInfoMapper.roomList(iPage, dataDTO.getData());

        PageResult<RoomInfo> pageResult = new PageResult<>();
        pageResult.setPages(pageInfo.getPages());
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setData(pageInfo.getRecords());
        return pageResult;
    }

    @Override
    public Result setRoomStatus(RoomInfo roomInfo) {
        if(roomInfo != null){
            int i = roomInfoMapper.setRoomStatus(roomInfo);
            if(i == 1){
                RoomInfo roomInfoBySearch = roomInfoMapper.selectById(roomInfo.getRoomId());

                SendResult sendResult = rocketMQTemplate.syncSend(esTopicName + ":" + "es-tag", roomInfoBySearch);
                System.out.println(sendResult);
                rocketMQTemplate.syncSend(pageTopicName + ":" + "page-tag", roomInfo.getRoomId());
                System.out.println(sendResult);
                return new Result(true, "设置成功", "");
            }else {
                return new Result(false, "设置失败", "");
            }
        }else {
            return new Result(false, "设置失败", "");
        }
    }

    @Override
    public Result setRoomStatusNoPass(RoomInfo roomInfo) {
        Result result = new Result();
        int i = roomInfoMapper.setRoomStatusNoPass(roomInfo);
        result.setSuccess(i > 0);
        result.setMessage(i > 0 ? "设置成功" : "设置失败");
        return null;
    }


    public static void main(String[] args) {

        //





    }

}
